// 网关配置的前缀
var jobsServer = "/jobsSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

// let CorHId = "400001";
let CorHId = "";
var search_value = '';

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    CorHId = $("#userId").html();
    console.log(CorHId)
    getHuntingStatus(currentPage, pageSize, search_value);
}

function getHuntingStatus(currentPage, pageSize, search_value) {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/huntingInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
            userId: CorHId
        },
        success: function (data) {
            console.log("所有的岗位申请状态数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            //清空数据
            $("#huntingList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                $("#huntingList").append(
                    "<tr class='job-application-item'>" +
                    "<td class='application-job'>" +
                    "<h3><a onclick='jobDetail(\""+item.jobId+"\", \""+item.companyId+"\")'>" + item.jobName + "</a></h3>" +
                    "</td>" +

                    "<td class='application-employer'>" +
                    "<a class='dotted'>" + item.userName + "</a>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span>" + item.createDate + "</span>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span>" + item.huntingStatus + "</span>" +
                    "</td>" +

                    "<td class='download-cv'>" +
                    "<a  onclick='userDetail(\"" + item.userId + "\", \"" + item.huntingId + "\", \"" + item.huntingStatus + "\")'>" +
                    "<i class='lnr lnr-eye'></i> 在线查看</a>" +
                    "</td>" +

                    "<td class='view-application-pop text-right'>" +
                    "<a onclick='updateStatus(\"" + item.huntingId + "\", \"" + item.huntingStatus + "\")'><span>操作</span></a>" +
                    "</td>" +
                    "</tr>"
                );
            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getHuntingStatus(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

function userDetail(userId, huntingId, status) {
    if (status === "已送达"){
        alert(status);
        $.ajax({
            type: "POST",
            async: false,
            url: jobsServer + "/jobs/huntingInfo",
            headers: {
                "Content-Type": "application/json",
                "Authorization": Authorization,
                "refreshToken": refreshToken,
            },
            data: JSON.stringify({
                "huntingId": huntingId,
                "huntingStatus": "已查阅"
            }),
            success: function (data) {
                console.log(data.resp_msg);
            },
            error: function (err) {
                // 异常
                console.log(err);
            }
        })
    }
    location.href = "/oa/jobs/CandidateDetailsPage/" + userId + "/" + CorHId + "/" + huntingId;
}

//  搜索框
function click_search() {
    var tmp = $('#candidate_search').val();
    if (tmp === "" || tmp === null) {
        tmp = "";
    }
    search_value = tmp;
    getHuntingStatus(1, pageSize, search_value);
}

function updateStatus(huntingId, huntingStatus) {
    $.confirm({
        title: '提示',
        content: function () {
            var self = this;
            return $.ajax({
                url: jobsServer + '/jobs/huntingStatus',
                dataType: 'json',
                method: 'get'
            }).done(function (response) {
                self.setContent('确定修改当前记录的状态吗？<br/>');
                self.setContentAppend('请选择状态<br/>');
                let option = ""
                $.each(response.datas, function (i, item) {
                    let value = item.value;

                    if (huntingStatus === "邀面试") {
                        option = "<option value='邀面试' selected>已经邀请该用户面试，不能修改</option>"
                        return false;
                    }

                    if (value === huntingStatus) {
                        option += "<option value='" + item.value + "' selected>" + item.value + "</option>"
                    } else if (value === "已送达") {
                        option += "";
                    } else {
                        option += "<option value='" + item.value + "'>" + item.value + "</option>"
                    }
                })
                self.setContentAppend(
                    "<select class='nice-select wide' id='status'>" +
                    option +
                    "</select>"
                );
            }).fail(function () {
                self.setContent('Something went wrong.');
            });
        },
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-warning',
                action: function () {
                    let status = $("#status").selected().val();
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: jobsServer + "/jobs/huntingInfo",
                        headers: {
                            "Content-Type": "application/json",
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        data: JSON.stringify({
                            "huntingId": huntingId,
                            "huntingStatus": status
                        }),
                        success: function (data) {
                            if (data.resp_code === 0) {
                                // 成功
                                location.reload();
                            } else {
                                // 失败
                                cocoMessage.error(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                            }
                        },
                        error: function (err) {
                            // 异常
                            cocoMessage.error(2000, err.statusText, function () {
                                console.log(err);
                            });
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}
function jobDetail(jobId, companyId){
    location.href ="/oa/jobs/jobDetails/"+jobId+"/"+companyId;
}
